package com.example.demo.controller;

import com.example.demo.entity.Stranger;
import com.example.demo.exception.BizException;
import com.example.demo.service.StrangerService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stranger/")
public class StrangerController {
    private static final Logger log = LoggerFactory.getLogger(StrangerController.class);


    @Autowired
    private StrangerService strangerService;

    /**
     * 查询路人信息列表
     * @return
     */
    @ApiOperation(value = "查询路人信息列表", notes = "数据")
    @RequestMapping(value = "getStrangerList",method = RequestMethod.GET)
    public List<Stranger> getMessageList(@RequestBody Stranger stranger){
        return strangerService.getStrangerList(stranger);
    }

    /**
     * 新增消息
     * 表单直接提交的数据，无需转为 json格式
     * @param
     * @return
     */
    @ApiOperation(value = "新增消息", notes = "数据")
    @RequestMapping(value = "addStranger",method = RequestMethod.POST)
    public List<Stranger> addMessage(Stranger stranger){
        if(ObjectUtils.isEmpty(stranger)){
            throw new BizException();
        }
        try {
            strangerService.addStranger(stranger);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return strangerService.getStrangerList(null);
    }

}
